package com.dream.keigezhushou.Activity.activity;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dream.keigezhushou.Activity.Uiutils.UiUtils;
import com.dream.keigezhushou.Activity.bean.SongInfo;
import com.dream.keigezhushou.Activity.pop.MoreWindowToMusicMessage;
import com.dream.keigezhushou.R;
import com.skyfishjy.library.RippleBackground;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SongHelperActivity extends BaseActivity implements View.OnClickListener {


    @BindView(R.id.back)
    ImageView mBack;
    @BindView(R.id.discern)
    TextView mDiscern;

    @BindView(R.id.content)
    RippleBackground rippleBackground;

    @BindView(R.id.view1)
    View mView1;
    @BindView(R.id.discern_rl)
    RelativeLayout mDiscernRl;
    @BindView(R.id.discernhistory)
    TextView mDiscernhistory;
    @BindView(R.id.view2)
    View mView2;
    @BindView(R.id.discern_history)
    RelativeLayout mDiscernHistory;
    @BindView(R.id.tv)
    TextView mTv;
    @BindView(R.id.iv)
    ImageView mIv;
    @BindView(R.id.text)
    TextView mText;
    @BindView(R.id.rl)
    RelativeLayout mRl;

    @BindView(R.id.layout)
    LinearLayout mLayout;
    @BindView(R.id.tautology)
    TextView mTautology;
    @BindView(R.id.btn)
    RelativeLayout mBtn;
    @BindView(R.id.wsb)
    FrameLayout mWsb;
    @BindView(R.id.lvlist)
    ListView mLvlist;
    @BindView(R.id.fl)
    FrameLayout mFl;
    @BindView(R.id.activity_song_helper)
    LinearLayout mActivitySongHelper;
    private  ArrayList<SongInfo> list = new ArrayList<>();
    private PopupWindow mMPopWindow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_song_helper);
        ButterKnife.bind(this);
        initdata();
        mDiscern.setSelected(true);
        mView1.setVisibility(View.VISIBLE);
        mTv.setVisibility(View.VISIBLE);

        final Handler handler = new Handler();


        mRl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rippleBackground.startRippleAnimation();
                mTv.setVisibility(View.GONE);
                mText.setText("停止识别");
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        foundDevice();


                    }
                }, 3000);
            }
        });



        mLvlist.setAdapter(new SongAdapter());

        mLvlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //跳到歌词播放页面
                //识别出来的歌曲也跳同样的页面
                Intent intent = new Intent(SongHelperActivity.this,SongLyricActivity.class);

                startActivity(intent);
            }
        });

    }

    private void initdata() {
        for(int i=0;i<20;i++){
            SongInfo info = new SongInfo();
            info.setName("一直很安静");
            info.setAuthor("阿桑");
list.add(info);
        }
    }

    private void foundDevice() {


        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(100);
        animatorSet.setInterpolator(new AccelerateDecelerateInterpolator());
        ArrayList<Animator> animatorList = new ArrayList<Animator>();
        ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(mIv, "ScaleX", 0f, 1.2f, 1f);
        animatorList.add(scaleXAnimator);
        ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(mIv, "ScaleY", 0f, 1.2f, 1f);
        animatorList.add(scaleYAnimator);
        animatorSet.playTogether(animatorList);
        mIv.setVisibility(View.VISIBLE);
        animatorSet.start();

        // startActivity(new Intent(SongHelperActivity.this,WeiDiscernActivity.class));


    }


    @OnClick({R.id.discern_rl, R.id.discern_history, R.id.rl})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.discern_rl:
                mLayout.setVisibility(View.VISIBLE);
                mView1.setVisibility(View.VISIBLE);
                mFl.setVisibility(View.GONE);
                mDiscern.setSelected(true);

                mDiscernhistory.setSelected(false);
                mView2.setVisibility(View.GONE);
                break;
            case R.id.discern_history:
                mLayout.setVisibility(View.GONE);
                mDiscern.setSelected(false);
                mView1.setVisibility(View.GONE);
                mDiscernhistory.setSelected(true);
                mView2.setVisibility(View.VISIBLE);
                setLayout();

                break;
            case R.id.rl:
                break;
            case R.id.l_downLoad:
                UiUtils.toast("下载");
                break;
            case R.id.l_singCounts:
                UiUtils.toast("唱过");
                break;
            case R.id.l_addToCollecte:
                UiUtils.toast("收藏成功");
                break;
            case R.id.l_share:
                UiUtils.toast("分享");
                break;
            case R.id.l_message:
                UiUtils.toast("歌曲");
                WindowManager wm = (WindowManager) SongHelperActivity.this
                        .getSystemService(Context.WINDOW_SERVICE);

                int width = wm.getDefaultDisplay().getWidth();
                int height = wm.getDefaultDisplay().getHeight();


                MoreWindowToMusicMessage moreWindowToMusicMessage = new MoreWindowToMusicMessage(View.inflate(SongHelperActivity.this, R.layout.pop_musicmessage, null),SongHelperActivity.this, width, ViewGroup.LayoutParams.WRAP_CONTENT);
//                       moreWindow.setBackColorState(0);
                moreWindowToMusicMessage.showPopAtLocation(mLvlist, Gravity.BOTTOM);
                mMPopWindow.dismiss();
                break;
            case R.id.l_delete:
                UiUtils.toast("删除");
                break;

        }
    }

    private void setLayout() {
        mLayout.setVisibility(View.INVISIBLE);
        mFl.setVisibility(View.VISIBLE);


    }

  class SongAdapter extends BaseAdapter {



      @Override
      public int getCount() {
          return list.size();
      }

      @Override
      public SongInfo getItem(int position) {
          return list.get(position);
      }

      @Override
      public long getItemId(int position) {
          return position;
      }

      @Override
      public View getView(int position, View convertView, ViewGroup parent) {

          ViewHolder holder=null;
          if(convertView==null){
           convertView = UiUtils.inflateView(R.layout.collected_music_list_item);

              holder = new ViewHolder();
              holder.name = (TextView) convertView.findViewById(R.id.collected_music_name);
              holder.author = (TextView) convertView.findViewById(R.id.collected_music_singer);
              holder.iv = (ImageView) convertView.findViewById(R.id.item_more);
              convertView.setTag(holder);
          }else{
              holder = (ViewHolder) convertView.getTag();
          }

          SongInfo info = getItem(position);
          holder.name.setText(info.getName());
          holder.author.setText(info.getAuthor());
          holder.iv.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  showPopupWindow();

              }
          });

          return convertView;
      }
      private void showPopupWindow() {
      //  View contentView = LayoutInflater.from(SongHelperActivity.this).inflate(R.layout.golde30s_pupwindow, null);
       //   View contentView = LayoutInflater.from(SongHelperActivity.this).inflate(R.layout.pop_local, null);
          View contentView = LayoutInflater.from(SongHelperActivity.this).inflate(R.layout.popupwindow_layout, null);
          mMPopWindow = new PopupWindow(contentView,
                  LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
          mMPopWindow.setContentView(contentView);
          LinearLayout ll= (LinearLayout) contentView.findViewById(R.id.l_downLoad);
          LinearLayout singCounts= (LinearLayout) contentView.findViewById(R.id.l_singCounts);
          LinearLayout addToCollecte= (LinearLayout) contentView.findViewById(R.id.l_addToCollecte);
          LinearLayout share= (LinearLayout) contentView.findViewById(R.id.l_share);
          LinearLayout message= (LinearLayout) contentView.findViewById(R.id.l_message);
          LinearLayout l_delete= (LinearLayout) contentView.findViewById(R.id.l_delete);

          ll.setOnClickListener(SongHelperActivity.this);
          singCounts.setOnClickListener(SongHelperActivity.this);
          share.setOnClickListener(SongHelperActivity.this);
          addToCollecte.setOnClickListener(SongHelperActivity.this);
          message.setOnClickListener(SongHelperActivity.this);
          l_delete.setOnClickListener(SongHelperActivity.this);
          View rootview = LayoutInflater.from(SongHelperActivity.this).inflate(R.layout.activity_song_helper, null);
          mMPopWindow.showAtLocation(rootview, Gravity.BOTTOM, 0, 0);


      }
  }


    static  class ViewHolder{
        TextView name;
        TextView author;
        ImageView iv;
    }
}
